package org.lc.stk.dao.question;

import java.util.List;

import org.lc.stk.model.question.attrs.QuestionType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface QuestionTypeRepository extends JpaRepository<QuestionType, Long> {
    
    /**
     * 检查名称是否存在（排除指定ID）
     */
    @Query("SELECT COUNT(t) > 0 FROM QuestionType t WHERE t.name = :name AND (:excludeId IS NULL OR t.id != :excludeId)")
    boolean existsByNameAndIdNot(
        @Param("name") String name,
        @Param("excludeId") Long excludeId
    );

    /**
     * 获取所有系统内置题型
     */
    @Query("SELECT t FROM QuestionType t WHERE TYPE(t) = SystemQuestionType")
    List<QuestionType> findAllSystemTypes();

    /**
     * 获取所有用户自定义题型
     */
    @Query("SELECT t FROM QuestionType t WHERE TYPE(t) = UserQuestionType")
    List<QuestionType> findAllUserDefinedTypes();

    /**
     * 检查指定ID列表的题型是否都存在
     */
    @Query("SELECT COUNT(t) = :totalCount FROM QuestionType t WHERE t.id IN :ids")
    boolean existsAllByIds(@Param("ids") List<Long> ids, @Param("totalCount") long totalCount);
}